package com.example.hello.controller;

import com.example.hello.pojo.*;
import com.example.hello.service.BookRepositoryService;
import com.example.hello.service.BorrowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/repository")
public class BookRepositoryController {


    @Autowired
    private BookRepositoryService bookRepositoryService;
    @Autowired
    private BorrowService borrowService;

    @PostMapping
    public Res save(@RequestBody Book book) {
        log.info("新添加的书籍信息为： {}", book);
        bookRepositoryService.save(book);
        return Res.success();
    }

    @GetMapping("/list")
    public Res findAll(
            PageQueryParam bookQueryParam
    ) {
        log.info("查找书籍所有的库存,查询信息为：{}",bookQueryParam);
        PageResult<Book> bookList = bookRepositoryService.findAll(bookQueryParam);
        return Res.success(bookList);
    }

    @GetMapping("/listOrderByBorrowNum")
    public Res findAllOrderByBorrowNum(
            PageQueryParam bookQueryParam
    ) {
        log.info("查找书籍所有的库存,查询信息为：{}",bookQueryParam);
        PageResult<Book> bookList = bookRepositoryService.findAllOrderByBorrowNum(bookQueryParam);
        return Res.success(bookList);
    }

    @GetMapping
    public Res findByTitleOrAuthorOrISBN(
            String requestParam
    ) {
        log.info("用户的查阅信息为： {}", requestParam);
        List<Book> bookList = bookRepositoryService.findByTitleOrAuthorOrISBN(requestParam);
        return Res.success(bookList);
    }

    /**
     * 借阅图书接口
     * URL: /repository/borrow
     * 请求方式: POST
     * 参数: userId（用户ID）、bookIsbn（图书Isbn）
     */
    @PostMapping("/borrow")
    public Res borrowBook(@RequestParam("userId") Long userId,
                          @RequestParam("bookId") Integer bookId) {
        try {
            UserBook userBook = borrowService.borrowBook(userId, bookId);
            return Res.success("借阅成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Res.error(e.getMessage());
        }
    }

    /**
     * 根据用户ID查询该用户借阅的所有图书
     * URL 示例：/repository/borrow/list?userId=1
     */
    @GetMapping("/borrow/list")
    public Res getBorrowedBooks(@RequestParam("userId") Long userId) {
        try {
            List<Book> books = borrowService.getBorrowedBooksByUserId(userId);

            return Res.success(books);
        } catch (Exception e) {
            e.printStackTrace();
            return Res.error("查询借阅记录失败：" + e.getMessage());
        }
    }

    @GetMapping("/borrowed/list")
    public Res getAllBorrowedBooks(@RequestParam("userId") Long userId) {
        try {
            List<Book> books = borrowService.getAllBorrowedBooksByUserId(userId);

            return Res.success(books);
        } catch (Exception e) {
            e.printStackTrace();
            return Res.error("查询借阅记录失败：" + e.getMessage());
        }
    }

    @GetMapping("/returned/list")
    public Res getAllReturnedBooks(@RequestParam("userId") Long userId) {
        try {
            List<Book> books = borrowService.getAllReturnedBooksByUserId(userId);

            return Res.success(books);
        } catch (Exception e) {
            e.printStackTrace();
            return Res.error("查询借阅记录失败：" + e.getMessage());
        }
    }

    /**
     * 还书接口
     * 请求方式：POST
     * 参数：userId（用户ID）、bookId（图书ID）
     *
     * @param userId 用户ID
     * @param bookId 图书ID
     * @return 返回还书操作结果
     */
    @PostMapping("/return")
    public Res returnBook(@RequestParam("userId") Long userId,
                                  @RequestParam("bookId") Integer bookId) {
        return borrowService.returnBook(userId, bookId);
    }

    @PostMapping("/borrowByQrcode")
    public Res findBookIdByIsbn(@RequestParam("userId") Long userId,
                          @RequestParam("bookIsbn") String bookIsbn) {
        try {
            Integer bookId = borrowService.findBookIdByIsbn(userId, bookIsbn);
            return Res.success("借阅成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Res.error(e.getMessage());
        }
    }


}
